package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import bdd.beans.Enseignant;
import bdd.beans.Indisponibilites;
import bdddaosimpl.DAOEnseignantImpl;
import bdddaosimpl.DAOIndisponibiliteImpl;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author anwarend
 */
public class FicheVoeuxAdminServlet extends HttpServlet {

   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    DAOEnseignantImpl e = new DAOEnseignantImpl();
    List<Enseignant> listeEnseignants = e.findAll();
    request.setAttribute("listeEnseignants", listeEnseignants);
    // On re-route l'appel vers la page d'accueil JSP
    gotoPage("/fiche_voeux_admin.jsp", request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("afficher")!=null)
        {
           DAOIndisponibiliteImpl e = new DAOIndisponibiliteImpl();
           int i = Integer.parseInt(request.getParameter("liste"));
           List<Indisponibilites> listeFicheVoeux = e.findByEnseignant(i);
           request.setAttribute("listeFicheVoeux", listeFicheVoeux);
           request.setAttribute("number", 1);
        }       
        // On re-route l'appel vers la page d'accueil JSP
        DAOEnseignantImpl e = new DAOEnseignantImpl();
        List<Enseignant> listeEnseignants = e.findAll();
        Enseignant en = e.find(Integer.parseInt(request.getParameter("liste")));
        request.setAttribute("identite", en.getPrenom()+" "+en.getNom());
        request.setAttribute("listeEnseignants", listeEnseignants);
        gotoPage("/fiche_voeux_admin.jsp", request, response);
        
    }


     private void gotoPage(String adress, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(adress);
    dispatcher.forward(request,response);
  }
}
